<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Storage;

class weiboHomeController extends Controller
{
    /**
     * 首页显示
     */
    public function getIndex()
    {

        /**
         *  微博查询
         * @李明坤
         */
        $res = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> orderBy('subTime','desc')
            -> get();

        //随机板块查询
        $types = DB::table('types')
            -> orderBy(\DB::raw('RAND()'))
            -> take(10)
            -> get();
        //$types = DB::select('select * from types order by rand() limit 10');

        //苑卫强
        $userid = session('id');

        $nickName = DB::table('user_detail')->where('user_detail.id',$userid)->first();

        $prov = DB::table('address')->where('address.uid',$userid)->first();


        return view('homes.Customerindex',['nickName'=>$nickName, 'prov'=>$prov, 'weibos' => $res, 'types'=>$types]);

    }


    /*
     * 前台微博(js)发布
     * @李明坤
     */
    public function postAdd(Request $request)
    {
        //var_dump($request -> all());die;
        //formData上传图片操作
        $prefix = "images".mt_rand(1000,9999);

        if (!empty($request -> images)) {
            //获取图片名
            $Origname = $request->images->getClientOriginalName();

            //组合新图片名
            $name = $prefix . $Origname;

            $src = '/uploads/weibo/' . $name;

            //临时文件目录
            $realPath = $request->images->getRealPath();

            //存储图片
            Storage::disk('uploads')->put($src, file_get_contents($realPath));
        } else {
            $src = "";
        }

        //视频上传操作
        $prefix = "video".mt_rand(1000,9999);

        if (!empty($request -> video)) {
            //获取图片名
            $Origname = $request->video->getClientOriginalName();

            //组合新图片名
            $name = $prefix . $Origname;

            $video = '/uploads/video/' . $name;

            //临时文件目录
            $realPath = $request->video->getRealPath();

            //存储图片
            Storage::disk('uploads')->put($video, file_get_contents($realPath));
        } else {
            $video = "";
        }

        //获取发布的内容
        if (!empty($request -> str)) {
            $content = $request->str;
        } else {
            $content = "";
        }

        //获取用户id
        $id = session('id');

        //判断用户是否登录
        if (empty($id)) {
            return redirect('login');
        }

        //热点
        $keywords = $request -> keywords;
        if (empty($keywords)) {
            $keywords = "微博";
        }
        $time = time();

        $res = DB::table('types') -> where('typeName','=',$keywords) -> first();

        //判断关键字 是否已经存在,存在则不写入types表,直接检索读取,不存在则写入并且再读取
        if (empty($res)) {
            $tid = DB::table('types')
                -> insertGetId(['typeName' => $keywords]);
            $n = 1;
        } else {
            $r = DB::table('types')
                -> where('typeName',$keywords)
                -> get();

            $tid = $r[0]['tid'];
            $num = DB::table('types')
                -> where('tid',$tid)
                -> select('count')
                ->first();
            //热点计数
            $n = $num['count'] + 1;
        }

        //写入微博数据库
        $pid = DB::table('posts')
            -> insertGetId([
                'uid' => $id,
                'tid' => $tid,
                'content' => $content,
                'subTime' => $time,
                'img' => $src,
                'video' => $video
            ]);

        //写入热点计数
        DB::update("update types set count =$n where tid=$tid");

        //写入微博图片库
        DB::table('images')
            -> insert([
                'uid' => $id,
                'tid' => $tid,
                'pid' => $pid,
                'image' => $src
            ]);

        //写入视频数据库
        DB::table('video')
            -> insert([
                'uid' => $id,
                'tid' => $tid,
                'pid' => $pid,
                'video' => $video
            ]);

        echo "1";

        /*DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> where('uid',$id)
            -> first();
        echo "1";*/


    }


    /**
     * 设置页面入口
     */
    public function getSet()
    {
        return view('homes.setting');
    }

    /**
     * 热点微博页面显示
     */
    public function getRedian($tid)
    {
        $id = session('id');

        if (empty($id)) {
            return redirect('/homes/login');
        }

        //查出此热点话题名
        $typeName = DB::table('types')
            -> where('tid',$tid)
            -> first();

        //查出所有的热点以方便遍历显示
        $types = DB::table('types')
            -> orderBy(\DB::raw('RAND()'))
            -> take(10)
            -> get();

        //查询出热点下的所有微博
        $res = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> where('tid',$tid)
            -> orderBy('subTime','desc')
            -> get();
/*echo "<pre>";
        var_dump($res);die;*/

        //遍历出热门话题id
        //$uid = [];
        //foreach ($res as $v) {
        //    $uid[] = $v['uid'];
        //}

        $nickName = DB::table('user_detail')
            ->where('user_detail.id',$id)
            ->first();

        $prov = DB::table('address')
            ->where('address.uid',$id)
            ->first();


        return view('homes.redian',['nickName'=>$nickName,'prov'=>$prov,'typeName' => $typeName,'weibos' => $res,'types' => $types]);
    }

    /**
     * 个人微博主页
     */
    public function getMyweb()
    {
        $id = session('id');

        if (empty($id)) {
            return view('homes.login');
        }

        $count = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> where('uid',$id)
            -> count();

        $caogao = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','0')
            -> where('uid',$id)
            -> count();

        $recycle = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','0')
            -> where('delete','=','1')
            -> where('uid',$id)
            -> count();

        $res = DB::table('posts')
            -> join('user_detail','posts.uid','=','user_detail.id')
            -> join('types','posts.tid','=','types.tid')
            -> where('recycle','=','1')
            -> where('delete','=','1')
            -> where('caogao','=','1')
            -> where('uid',$id)
            -> orderBy('subTime','desc')
            -> get();
        /*echo "<pre>";
        var_dump($res);die;*/
        //苑卫强
        $nickName = DB::table('user_detail')->where('user_detail.id',$id)->first();

        $prov = DB::table('address')->where('address.uid',$id)->first();


        return view('homes.MyWB',['nickName'=>$nickName, 'prov'=>$prov,'count'=>$count, 'weibo'=>$res, 'caogao' => $caogao, 'recycle'=>$recycle]);

    }


}
